package com.rock.platform.entity;

import java.util.ArrayList;
import java.util.List;


public class GridParamsEntity {

	private int start;
	private int limit;
	private int page;
	private String sort;
	private String filter;
	private List<FilterEntity> filterList = new ArrayList<FilterEntity>();
	private List<SortEntity> sortList = new ArrayList<SortEntity>();
	
	public int getStart() {
		return start;
	}
	public void setStart(int start) {
		this.start = start;
	}
	public int getLimit() {
		return limit;
	}
	public void setLimit(int limit) {
		this.limit = limit;
	}
	public int getPage() {
		return page;
	}
	public void setPage(int page) {
		this.page = page;
	}
	public String getSort() {
		return sort;
	}
	public void setSort(String sort) {
		this.sort = sort;
	}
	public String getFilter() {
		return filter;
	}
	public void setFilter(String filter) {
		this.filter = filter;
	}
	public List<SortEntity> getSortList() {
		return sortList;
	}
	public void setSortList(List<SortEntity> sortList) {
		this.sortList = sortList;
	}
	public List<FilterEntity> getFilterList() {
		return filterList;
	}
	public void setFilterList(List<FilterEntity> filterList) {
		this.filterList = filterList;
	}
	public GridParamsEntity addSort(String property, String direction)
	{
		boolean bExists = false;
		for (SortEntity sortEntity : this.sortList)
		{
			if (sortEntity.getField().trim().toLowerCase().equals(property.trim().toLowerCase()))
			{
				bExists = true;
				break;
			}
		}
		if (!bExists)
		{
			SortEntity e = new SortEntity();
			e.setField(property);
			e.setDirection(direction);
			this.sortList.add(e);
		}
		return this;
	}
	public GridParamsEntity addSort(String property)
	{
		return addSort(property," ASC ");
	}
	public GridParamsEntity replaceField(String oldField,String newField)
	{
		for(FilterEntity e : this.getFilterList())
		{
			if (e.getField().equals(oldField))
			{
				e.setField(newField);
				break;
			}
		}
		for(SortEntity e : this.getSortList())
		{
			if (e.getField().equals(oldField))
			{
				e.setField(newField);
				break;
			}
		}
		return this;
	}
}
